#!/usr/bin/env python

# Copyright 2012 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Registration response produced by IDP and consumed by AP."""

from openidconnect.error import DataError

__author__ = 'Maciej Machulak'
__maintainer__ = 'Maciej Machulak'
__email__ = 'mmachulak@google.com'

__copyright__ = 'Copyright 2012, Google Inc.'
__license__ = 'Apache License 2.0'
__version__ = '0.2'
__status__ = 'Prototype'


class RegistrationResponse(object):
  """Registration response produced by IDP and consumed by AP."""

  def __init__(self, response_dict, response):
    if not isinstance(response_dict, dict):
      raise DataError('invalid data format')
    self.id = response_dict.get('id', None)
    # TODO(mmachulak) either header or json? maybe leave json, easier to parse
    self.location = response_dict.get('location')
    if not self.location:
      self.location = response.get('Location', None)
    # TODO(mmachulak) maybe we should leave a dict, and have a common validator
    if not self.location:
      raise DataError('invalid data')

  def __repr__(self):
    rsp = 'id: ' + (self.id or '')
    rsp += '\nlocation: ' + (self.location or '')
    return rsp
